from rest_framework import serializers

from users.models import User
from .models import StoreManagement, StoreFinance, PerformanceChecklist, OrderDefects, CustomerExperience, \
    TrackingInformation, BuyerSellerContactResponseTimeMetrics, ReturnDissatisfaction, CustomerService, \
    PerformanceChecklistStatus, TimeFrame, RateAndCount, OrderDefectMetrics, CustomerExperienceMetrics, TrackingMetrics, \
    ResponseTimeMetrics, CustomerServiceMetrics, ReturnDissatisfactionMetrics, ProductAuthenticityData, \
    ProductSafetyData, ListingPolicyData, IntellectualPropertyData


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username']


class StoreListSerializer(serializers.ModelSerializer):
    staff = UserSerializer(read_only=True)

    class Meta:
        model = StoreManagement
        fields = ['id', 'staff', 'store', 'day_up_goods', 'day_flow', 'day_orders', 'total_flow',]


class NewStoreAddSerializer(serializers.ModelSerializer):

    class Meta:
        model = StoreManagement
        fields = ['store', "seller_id",'mws_token']


class DeleteStoreSerializer(serializers.ModelSerializer):

    class Meta:
        model = StoreManagement
        fields = ['store']


class UpdateStoreInfoSerializer(serializers.ModelSerializer):

    class Meta:
        model = StoreManagement
        fields = ['store', 'day_up_goods', 'day_flow', 'day_orders', 'total_flow',]






class PerformanceChecklistStatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = PerformanceChecklistStatus
        fields = ['status']


class TimeFrameSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimeFrame
        fields = ['start','end']

class RateAndCountSerializer(serializers.ModelSerializer):
    class Meta:
        model = RateAndCount
        fields = ['rate','count']


class PerformanceChecklistSerializer(serializers.ModelSerializer):

    class Meta:
        model = PerformanceChecklist
        fields = '__all__'


class OrderDefectsSerialzier(serializers.ModelSerializer):

    class Meta:
        model = OrderDefects
        fields = '__all__'

class OrderDefectMetricsSerialzier(serializers.ModelSerializer):

    class Meta:
        model = OrderDefectMetrics
        fields = '__all__'


class CustomerExperienceSerializer(serializers.ModelSerializer):

    class Meta:
        model = CustomerExperience
        fields = '__all__'


class CustomerExperienceMetricsSerializer(serializers.ModelSerializer):

    class Meta:
        model = CustomerExperienceMetrics
        fields = '__all__'


class TrackingInformationSerialzier(serializers.ModelSerializer):

    class Meta:
        model = TrackingInformation
        fields = '__all__'


class TrackingMetricsSerialzier(serializers.ModelSerializer):

    class Meta:
        model = TrackingMetrics
        fields = '__all__'


class BuyerSellerContactResponseTimeMetricsSerializer(serializers.ModelSerializer):
    class Meta:
        model = BuyerSellerContactResponseTimeMetrics
        fields = '__all__'


class ResponseTimeMetricsSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResponseTimeMetrics
        fields = '__all__'


class CustomerServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomerService
        fields = '__all__'


class CustomerServiceMetricsSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomerServiceMetrics
        fields = '__all__'


class ReturnDissatisfactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnDissatisfaction
        fields = '__all__'


class ReturnDissatisfactionMetricsSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnDissatisfactionMetrics
        fields = '__all__'


class ProductAuthenticityDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductAuthenticityData
        fields = '__all__'


class ProductSafetyDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductSafetyData
        fields = '__all__'


class ListingPolicyDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = ListingPolicyData
        fields = '__all__'


class IntellectualPropertyDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = IntellectualPropertyData
        fields = '__all__'



class PerformanceChecklistSerializer1(serializers.ModelSerializer):
    orderDefectRate = PerformanceChecklistStatusSerializer()
    orderCancellationRate = PerformanceChecklistStatusSerializer()
    lateShipmentRate = PerformanceChecklistStatusSerializer()
    policyViolation = PerformanceChecklistStatusSerializer()
    onTimeDelivery = PerformanceChecklistStatusSerializer()
    validTrackingRate = PerformanceChecklistStatusSerializer()
    contactResponseTime = PerformanceChecklistStatusSerializer()
    returnDissatisfactionRate = PerformanceChecklistStatusSerializer()
    customerServiceDissatisfactionRate = PerformanceChecklistStatusSerializer()
    productAuthenticityStatus = PerformanceChecklistStatusSerializer()
    productSafetyStatus = PerformanceChecklistStatusSerializer()
    listingPolicyStatus = PerformanceChecklistStatusSerializer()
    intellectualPropertyStatus = PerformanceChecklistStatusSerializer()

    class Meta:
        model = PerformanceChecklist
        fields = ['orderDefectRate', 'orderCancellationRate', 'lateShipmentRate', 'policyViolation', 'onTimeDelivery',
                  'validTrackingRate', 'contactResponseTime', 'returnDissatisfactionRate', 'customerServiceDissatisfactionRate',
                  'productAuthenticityStatus', 'productSafetyStatus', 'listingPolicyStatus', 'intellectualPropertyStatus']
        depth = 1




class OrderDefectMetricsSerialzier1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    orderWithDefects = RateAndCountSerializer()
    negativeFeedbacks = RateAndCountSerializer()
    a_z_claims = RateAndCountSerializer()
    chargebacks = RateAndCountSerializer()
    class Meta:
        model = OrderDefectMetrics
        fields = ['timeFrame', 'orderCount', 'orderWithDefects', 'negativeFeedbacks', 'a_z_claims', 'chargebacks']
        depth = 1

class OrderDefectsSerialzier1(serializers.ModelSerializer):
    orderDefectMetrics = OrderDefectMetricsSerialzier1(many=True)

    class Meta:
        model = OrderDefects
        fields = ['orderDefectMetrics']


class CustomerExperienceMetricsSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    lateShipment = RateAndCountSerializer()
    preFulfillmentCancellation = RateAndCountSerializer()
    refunds = RateAndCountSerializer()
    class Meta:
        model = CustomerExperienceMetrics
        fields = ['timeFrame', 'orderCount', 'lateShipment', 'preFulfillmentCancellation', 'refunds']
        depth = 1


class CustomerExperienceSerializer1(serializers.ModelSerializer):
    customerExperienceMetrics = CustomerExperienceMetricsSerializer1(many=True)
    class Meta:
        model = CustomerExperience
        fields = ['customerExperienceMetrics']



class TrackingMetricsSerialzier1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    validTracking = RateAndCountSerializer()
    onTimeDelivery = RateAndCountSerializer()

    class Meta:
        model = TrackingMetrics
        fields = ['timeFrame', 'requiredCategories', 'validTracking', 'onTimeDelivery']
        depth = 1


class TrackingInformationSerialzier1(serializers.ModelSerializer):
    trackingMetrics = TrackingMetricsSerialzier1(many=True)
    class Meta:
        model = TrackingInformation
        fields = ['trackingMetrics']


class ResponseTimeMetricsSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    class Meta:
        model = ResponseTimeMetrics
        fields = ['timeFrame','averageResponseTimeInHours','responseUnder24Hours','responseTimeGreaterThan24Hours','noResponseForContactsOlderThan24Hours' ]
        depth = 1


class BuyerSellerContactResponseTimeMetricsSerializer1(serializers.ModelSerializer):
    responseTimeMetrics = ResponseTimeMetricsSerializer1(many=True)
    class Meta:
        model = BuyerSellerContactResponseTimeMetrics
        fields = ['responseTimeMetrics']



class CustomerServiceMetricsSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    customerServiceDissatisfaction = RateAndCountSerializer()

    class Meta:
        model = CustomerServiceMetrics
        fields = ['timeFrame', 'customerServiceDissatisfaction']
        depth = 1


class CustomerServiceSerializer1(serializers.ModelSerializer):
    customerServiceMetrics = CustomerServiceMetricsSerializer1(many=True)
    class Meta:
        model = CustomerService
        fields = ['customerServiceMetrics']



class ReturnDissatisfactionMetricsSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    returnDissatisfaction = RateAndCountSerializer()
    negativeReturnFeedback = RateAndCountSerializer()
    lateResponse = RateAndCountSerializer()
    invalidRejection = RateAndCountSerializer()
    class Meta:
        model = ReturnDissatisfactionMetrics
        fields = ['timeFrame', 'returnOrderCount', 'returnDissatisfaction', 'negativeReturnFeedback', 'lateResponse', 'invalidRejection', ]
        depth = 1


class ReturnDissatisfactionSerializer1(serializers.ModelSerializer):
    returnDissatisfactionMetrics = ReturnDissatisfactionMetricsSerializer1(many=True)
    class Meta:
        model = ReturnDissatisfaction
        fields = ['returnDissatisfactionMetrics']



class ProductAuthenticityDataSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    class Meta:
        model = ProductAuthenticityData
        fields = ['timeFrame','defectCount']
        depth = 1


class ProductSafetyDataSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    class Meta:
        model = ProductSafetyData
        fields = ['timeFrame','defectCount']
        depth = 1


class ListingPolicyDataSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()
    class Meta:
        model = ListingPolicyData
        fields = ['timeFrame','defectCount']
        depth = 1


class IntellectualPropertyDataSerializer1(serializers.ModelSerializer):
    timeFrame = TimeFrameSerializer()

    class Meta:
        model = IntellectualPropertyData
        fields = ['timeFrame','defectCount']
        depth = 1






class SellerPerformanceReportSerializer(serializers.ModelSerializer):
    # performanceChecklist = PerformanceChecklistSerializer()
    # orderdefects_set = OrderDefectsSerialzier(many=True)
    # # customerexperience_set = CustomerExperienceSerializer(many=True)
    # # trackinginformation_set = TrackingInformationSerialzier(many=True)
    # # buyersellercontactresponsetimemetrics_set = BuyerSellerContactResponseTimeMetricsSerializer(many=True)
    # # customerservice_set = CustomerServiceSerializer(many=True)
    # # returndissatisfaction_set = ReturnDissatisfactionSerializer(many=True)
    # # productauthenticitydata_set = ProductAuthenticityDataSerializer()
    # # productsafetydata_set = ProductSafetyDataSerializer()
    # # listingpolicydata_set = ListingPolicyDataSerializer()
    # # intellectualpropertydata_set = IntellectualPropertyDataSerializer()
    performanceChecklist = PerformanceChecklistSerializer1()
    orderDefects = OrderDefectsSerialzier1(many=True)
    customerExperience = CustomerExperienceSerializer1(many=True)
    trackingInformation = TrackingInformationSerialzier1(many=True)
    buyerSellerContactResponseTimeMetrics = BuyerSellerContactResponseTimeMetricsSerializer1(many=True)
    customerService = CustomerServiceSerializer1(many=True)
    returnDissatisfaction = ReturnDissatisfactionSerializer1(many=True)
    productAuthenticityData = ProductAuthenticityDataSerializer1()
    productSafetyData = ProductSafetyDataSerializer1()
    listingPolicyData = ListingPolicyDataSerializer1()
    intellectualPropertyData = IntellectualPropertyDataSerializer1()

    class Meta:
        model = StoreFinance
        fields = ['report_id', 'performanceChecklist', 'orderDefects', 'customerExperience', 'trackingInformation',
                  'buyerSellerContactResponseTimeMetrics', 'customerService', 'returnDissatisfaction',
                  'productAuthenticityData','productSafetyData', 'listingPolicyData', 'intellectualPropertyData']



class StoreFinanceReportSerializer(serializers.ModelSerializer):
    PerformanceReport = SellerPerformanceReportSerializer(many=True)

    class Meta:
        model = StoreManagement
        fields = ['id','store','PerformanceReport']

